package com.roc.steelend.service.Impl;


import com.roc.steelend.controller.vo.common.BuyByRecoding;
import com.roc.steelend.controller.vo.common.InventoryByRecoding;
import com.roc.steelend.controller.vo.common.InventorySpecialReception;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.mapper.InventoryMapper;
import com.roc.steelend.pojo.BuyOrder;
import com.roc.steelend.pojo.Inventory;
import com.roc.steelend.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class InventoryServiceImpl  implements InventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;
    @Override
    public ReturnMsgForTable findJuanBanByKaiPing(Integer now,
                                                  Integer size,
                                                  Integer smallTypeId,
                                                  Integer warehouseId,
                                                  BigDecimal thickness,
                                                  BigDecimal weight) {
        ReturnMsgForTable msg = new ReturnMsgForTable();
        msg.setCurrenPage(now);
        now=(now-1)*size;
        msg.setObjectList(inventoryMapper.findInventoryByKaiping(now,size,smallTypeId,warehouseId,thickness,weight));
        msg.setTotal(inventoryMapper.findInventoryByKaipingCount(smallTypeId,warehouseId,thickness,weight));

        return msg;

    }

    @Override
    public List<HashMap<String, Object>> findInventoryById(Integer inventoryId) {
        return inventoryMapper.findInventoryById(inventoryId);
    }

    @Override
    public List<Inventory> inventoryFindRecoding(InventoryByRecoding selectMsg) {
        Integer now = selectMsg.getCurrentPage();
        Integer size = selectMsg.getPageSize();
        now=(now-1)*size;

        Integer bigTypeId = selectMsg.getBigTypeId();
        Integer smallTypeId = selectMsg.getSmallTypeId();
        Integer warehouseId = selectMsg.getWarehouseId();
        String specification = selectMsg.getSpecification();
        if (specification != null && !specification.equals("")){
            specification="%"+specification+"%";
        }
        Boolean showWarehouse3 = selectMsg.getShowWarehouse3();
        Boolean showOnlyWarehouse3 = selectMsg.getShowOnlyWarehouse3();

        return  inventoryMapper.findALlMsg(now,size,bigTypeId,smallTypeId,warehouseId,specification,showWarehouse3,showOnlyWarehouse3);
    }

    @Override
    public Integer inventoryFindRecodingCount(InventoryByRecoding selectMsg) {

        Integer now = selectMsg.getCurrentPage();
        Integer size = selectMsg.getPageSize();
        now=(now-1)*size;

        Integer bigTypeId = selectMsg.getBigTypeId();
        Integer smallTypeId = selectMsg.getSmallTypeId();
        Integer warehouseId = selectMsg.getWarehouseId();
        String specification = selectMsg.getSpecification();
        if (specification != null && !specification.equals("")){
            specification="%"+specification+"%";
        }
        Boolean showWarehouse3 = selectMsg.getShowWarehouse3();
        Boolean showOnlyWarehouse3 = selectMsg.getShowOnlyWarehouse3();

        return  inventoryMapper.inventoryFindRecodingCount(now,size,bigTypeId,smallTypeId,warehouseId,specification,showWarehouse3,showOnlyWarehouse3);

    }

    @Override
    public List<HashMap<String, Object>> daigangpingbanInventoryFindRecoding(InventorySpecialReception inventorySpecialReception) {

        Integer now = inventorySpecialReception.getCurrentPage();
        Integer size = inventorySpecialReception.getPageSize();
        Integer bigTypeId = inventorySpecialReception.getBigTypeId();
        Integer smallTypeId = inventorySpecialReception.getSmallTypeId();
        Integer warehouseId = inventorySpecialReception.getWarehouseId();
        BigDecimal thickness = inventorySpecialReception.getThickness();
        now=(now-1)*size;
        List<HashMap<String, Object>> hashMaps = inventoryMapper.daigangpingbanInventoryFindRecoding(now, size, bigTypeId, warehouseId, smallTypeId, thickness);

        List<HashMap<String,Object>> finallListMap = new ArrayList<>();
        for (int i = 0; i <hashMaps.size() ; i++) {
            HashMap<String, Object> hashMap = hashMaps.get(i);
            Integer smallTypeId1 = (Integer) hashMap.get("small_type_id");
            BigDecimal thickness1 = (BigDecimal)hashMap.get("thickness");
            List<HashMap<String, Object>> daigangpingbanInventoryFindRecodingDetailLIst = inventoryMapper.daigangpingbanInventoryFindRecodingDetailLIst(smallTypeId1, thickness1);
            hashMap.put("detailList",daigangpingbanInventoryFindRecodingDetailLIst);
            finallListMap.add(hashMap);
        }
        return finallListMap;

    }

    @Override
    public Integer daigangpingbanInventoryFindRecodingCount(InventorySpecialReception inventorySpecialReception) {
        Integer now = inventorySpecialReception.getCurrentPage();
        Integer size = inventorySpecialReception.getPageSize();
        Integer bigTypeId = inventorySpecialReception.getBigTypeId();
        Integer smallTypeId = inventorySpecialReception.getSmallTypeId();
        Integer warehouseId = inventorySpecialReception.getWarehouseId();
        BigDecimal thickness = inventorySpecialReception.getThickness();
        return inventoryMapper.daigangpingbanInventoryFindRecodingCount(bigTypeId,warehouseId,smallTypeId,thickness);
    }

    @Override
    public List<HashMap<String, Object>> inventoryWarehouse3() {
        return inventoryMapper.inventoryWarehouse3();
    }


}
